/**
 *
 * © Bahadir Zeren 2011
 * bzeren@gmail.com
 * No warranties, express or implied, are made for this program.
 *
 */
package org.baatar.opt.genetic.crossover;

import java.util.ArrayList;
import java.util.List;
import org.baatar.opt.genetic.chromosome.IChromosome;
import org.baatar.util.RandomUtil;

/**
 * Two point crossover implementation class.
 * 
 * @author Ba
 */
public class TwoPointCrossover<T> implements ICrossoverOperator<T> {
    @Override
    public List<IChromosome<T>> crossover(IChromosome<T> mother,
                                            IChromosome<T> father,
                                            double worstFitness) throws CloneNotSupportedException {
        List<IChromosome<T>> res = new ArrayList<IChromosome<T>>();

        IChromosome child1 = (IChromosome) mother.clone();
        IChromosome child2 = (IChromosome) father.clone();

        int crossoverPosition1 = RandomUtil.getRandNumber(mother.getChromosomeLength());
        int crossoverPosition2 = RandomUtil.getRandNumber(mother.getChromosomeLength() - crossoverPosition1) + crossoverPosition1;

        for (int i = crossoverPosition1; i < crossoverPosition2; i++) {
            child1.setGeneValue(i, father.getGeneValue(i));
            child2.setGeneValue(i, mother.getGeneValue(i));
        }

        res.add(child1);
        res.add(child2);

        return res;
    }
}
